
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第一章 搭建环境 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="1.写在前面的话.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="1.写在前面的话.html">
            
                <a href="1.写在前面的话.html">
            
                    
                    写在前面的话
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="2.搭建环境.html">
            
                <a href="2.搭建环境.html">
            
                    
                    第一章 搭建环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    第二章 模型定义
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.sequelize 实例变量.html">
            
                <a href="3.sequelize 实例变量.html">
            
                    
                    2.1 实例变量方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="4.定义一个模型.html">
            
                <a href="4.定义一个模型.html">
            
                    
                    2.2 定义一个模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="5.模型字段类型.html">
            
                <a href="5.模型字段类型.html">
            
                    
                    2.3 模型字段类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="6.导入与配置.html">
            
                <a href="6.导入与配置.html">
            
                    
                    2.3 导入与配置
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="3模型关系.html">
            
                <a href="3模型关系.html">
            
                    
                    第三章 模型关系
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="3.1属于.html">
            
                <a href="3.1属于.html">
            
                    
                    3.1 属于
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="3.2包含.html">
            
                <a href="3.2包含.html">
            
                    
                    3.2 包含
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="3.3一对多.html">
            
                <a href="3.3一对多.html">
            
                    
                    3.3 一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="3.4多对多.html">
            
                <a href="3.4多对多.html">
            
                    
                    3.4 多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="3.5多态一对多.html">
            
                <a href="3.5多态一对多.html">
            
                    
                    3.5 多态一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="3.6多态多对多.html">
            
                <a href="3.6多态多对多.html">
            
                    
                    3.6 多态多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="3.7关系嵌套.html">
            
                <a href="3.7关系嵌套.html">
            
                    
                    3.7 关系嵌套
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="4模型查询.html">
            
                <a href="4模型查询.html">
            
                    
                    第四章 模型查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="5查询范围.html">
            
                <a href="5查询范围.html">
            
                    
                    第五章 查询范围 Scope (预定义查询条件)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="6生命周期函数.html">
            
                <a href="6生命周期函数.html">
            
                    
                    第六章 生命周期函数 Hook
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="7migrate.html">
            
                <a href="7migrate.html">
            
                    
                    第七章 数据库迁移 Migrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="8tabel2model.html">
            
                <a href="8tabel2model.html">
            
                    
                    第八章 根据 Table 生成 Model
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第一章 搭建环境</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x642D;&#x5EFA;&#x73AF;&#x5883;">&#x642D;&#x5EFA;&#x73AF;&#x5883;</h1>
<p>&#x672C;&#x4E66;&#x6240;&#x6709;&#x7684;&#x4F8B;&#x5B50;&#x90FD;&#x5C06;&#x4F7F;&#x7528; <code>TypeScript</code> &#x8BED;&#x8A00;&#x8FDB;&#x884C;&#x7F16;&#x5199;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x4E0D;&#x4EC5;&#x53EF;&#x4EE5;&#x5F97;&#x5230;&#x4EE3;&#x7801;&#x63D0;&#x793A;&#xFF0C;&#x800C;&#x4E14;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>async/await</code> &#x7279;&#x6027;</p>
<p><strong>1.&#x521B;&#x5EFA;&#x9879;&#x76EE;&#x6587;&#x4EF6;&#x5939;</strong></p>
<pre><code>mkdir myApp &amp;&amp; cd myApp
</code></pre><p><strong>2.&#x521D;&#x59CB;&#x5316;&#x6211;&#x4EEC;&#x7684;&#x9879;&#x76EE;</strong></p>
<p>&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;<code>package.json</code>&#xFF0C;&#x6765;&#x4FDD;&#x5B58;&#x6211;&#x4EEC;&#x6240;&#x6709;&#x5B89;&#x88C5;&#x7684;&#x4F9D;&#x8D56;&#x5E93;&#x3002;</p>
<pre><code class="lang-bash">npm init -y
</code></pre>
<p><strong>3.&#x521D;&#x59CB;&#x5316; typescript &#x914D;&#x7F6E;&#x6587;&#x4EF6;</strong></p>
<p>tsc (typescript compiler) &#x662F; typescript &#x63D0;&#x4F9B;&#x7684;&#x547D;&#x4EE4;&#x884C;&#x5DE5;&#x5177;&#xFF0C;&#x7528;&#x4E8E;&#x7F16;&#x8BD1; <code>.ts</code> &#x6587;&#x4EF6;</p>
<pre><code class="lang-bash">tsc --init
</code></pre>
<p>&#x5C06; <code>tsconfig.json</code> &#x7684; <code>target</code> &#x6539;&#x6210; <code>es6</code></p>
<p><strong>4.&#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x9879;</strong></p>
<p>&#x9996;&#x5148;&#x5B89;&#x88C5;&#x8FD0;&#x884C;&#x4F9D;&#x8D56;&#x9879;&#x76EE;</p>
<pre><code class="lang-bash">npm i sequelize mysql2 -S
</code></pre>
<p>&#x63A5;&#x7740;&#x5B89;&#x88C5;&#x5F00;&#x53D1;&#x4F9D;&#x8D56;</p>
<pre><code class="lang-bash">npm i @types/sequelize -D
</code></pre>
<p><strong>5.&#x51C6;&#x5907;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;</strong></p>
<p>&#x81EA;&#x884C;&#x51C6;&#x5907;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x6211;&#x8FD9;&#x91CC;&#x51C6;&#x5907;&#x7684;&#x6570;&#x636E;&#x5E93;&#x540D;&#x79F0;&#x53EB; <code>nodelover</code> </p>
<p><strong>6.&#x7F16;&#x5199;&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;</strong></p>
<p>&#x65B0;&#x5EFA;<code>index.ts</code></p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> Sequelize from <span class="hljs-string">&apos;sequelize&apos;</span>;

<span class="hljs-keyword">const</span> sequelize = <span class="hljs-keyword">new</span> Sequelize(<span class="hljs-string">&apos;nodelover&apos;</span>, <span class="hljs-string">&apos;root&apos;</span>, <span class="hljs-string">&apos;&apos;</span>, {
  host: <span class="hljs-string">&apos;localhost&apos;</span>,
  dialect: <span class="hljs-string">&apos;mysql&apos;</span>,

  pool: {
    max: <span class="hljs-number">5</span>,
    min: <span class="hljs-number">0</span>,
    idle: <span class="hljs-number">10000</span> <span class="hljs-comment">// &#x8FDE;&#x63A5;&#x5728;&#x91CA;&#x653E;&#x4E4B;&#x524D;&#x53EF;&#x4EE5;&#x7A7A;&#x95F2;&#x7684;&#x6700;&#x957F;&#x65F6;&#x95F4;&#xFF08;&#x4EE5;&#x6BEB;&#x79D2;&#x4E3A;&#x5355;&#x4F4D;&#xFF09;</span>
  },
});
</code></pre>
<p>&#x6B64;&#x65F6;&#x662F;&#x4F1A;&#x62A5;&#x9519;&#x7684;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x7684;&#x5B9A;&#x4E49;&#x6587;&#x4EF6;&#x6CA1;&#x6709;&#x9ED8;&#x8BA4;&#x5BFC;&#x51FA;&#x3002;</p>
<p><strong>&#x89E3;&#x51B3;&#x62A5;&#x9519;&#x7684;&#x95EE;&#x9898;</strong></p>
<p>1.&#x65B9;&#x6848;&#x4E00;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> * as Sequelize from <span class="hljs-string">&apos;sequelize&apos;</span>;
</code></pre>
<p>2.&#x65B9;&#x6848;&#x4E8C;
&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x4E00;&#x4E0B; <code>node_modules/sequelize/lib/sequelize.js</code> &#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x7684;&#x6700;&#x540E;&#x9762;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = Sequelize;
<span class="hljs-built_in">module</span>.exports.Sequelize = Sequelize;
<span class="hljs-built_in">module</span>.exports.default = Sequelize;
</code></pre>
<p>&#x5176;&#x5B9E;&#x5728;&#x5B98;&#x65B9;&#x5DF2;&#x7ECF;&#x505A;&#x4E86; es6 &#x7684;&#x517C;&#x5BB9;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x6211;&#x4EEC;&#x7684; @types &#x6587;&#x4EF6;&#x3002;</p>
<p>&#x6765;&#x5230; <code>node_modules/@types/sequelize/index.d.ts</code> &#x6587;&#x4EF6;</p>
<p>&#x628A;&#x6587;&#x4EF6;<strong>&#x672B;&#x5C3E;</strong>&#x7684;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> = sequelize;
</code></pre>
<p>&#x6539;&#x6210;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span>
</code></pre>
<blockquote>
<p>&#x8FD9;&#x91CC;&#x6211;&#x4F1A;&#x9009;&#x62E9;&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x6848;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x6BD4;&#x8F83;&#x559C;&#x6B22;&#x65B0;&#x7684;&#x4E1C;&#x897F;&#xFF0C;&#x6709;&#x4EBA;&#x8BF4;&#x91CD;&#x65B0;&#x5B89;&#x88C5; @types &#x4E4B;&#x540E;&#xFF0C;&#x6CA1;&#x6709;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#x65E0;&#x6CD5;&#x7F16;&#x8BD1;&#x600E;&#x4E48;&#x529E;&#xFF1F; &#x4E00;&#x822C;&#x8FD0;&#x884C;&#x7684;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x4E0A;&#x4F20; js &#x6587;&#x4EF6;&#x5373;&#x53EF;&#xFF0C;&#x800C;&#x5047;&#x5982;&#x662F;&#x5F00;&#x6E90;&#x7ED9;&#x522B;&#x4EBA;&#x770B;&#xFF0C;&#x53EF;&#x4EE5;&#x5728; README.md &#x6CE8;&#x660E;&#x4E00;&#x4E0B;&#x3002;</p>
</blockquote>
<p><strong>7.&#x5B89;&#x88C5;&#x4E00;&#x4E2A; ts &#x7684;&#x8FD0;&#x884C;&#x5DE5;&#x5177;</strong></p>
<pre><code class="lang-bash">npm i ts-node -g
</code></pre>
<p><strong>8.&#x6D4B;&#x8BD5;&#x8FDE;&#x63A5;&#x662F;&#x5426;&#x6210;&#x529F;</strong></p>
<pre><code class="lang-ts">async <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">main</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">try</span> {
    await sequelize.authenticate()
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;sequelize &#x5DF2;&#x7ECF;&#x8FDE;&#x63A5;&#x6210;&#x529F;&#x5566;&#xFF01; &gt;_&lt; &quot;</span>)
    process.exit(<span class="hljs-number">0</span>)
  }<span class="hljs-keyword">catch</span>(e){
    <span class="hljs-built_in">console</span>.log(e)
  }
}

main()
</code></pre>
<p><code>process.exit(0)</code> &#x662F;&#x4E3A;&#x4E86;&#x8BA9;&#x7A0B;&#x5E8F;&#x9000;&#x51FA;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x6211;&#x4EEC;&#x7684;&#x8FD0;&#x884C;&#x547D;&#x4EE4;&#x4E0E;&#x7ED3;&#x679C;&#x3002;</p>
<pre><code class="lang-bash">&gt; Yugo @ Tractor <span class="hljs-keyword">in</span> ~/Desktop/nl-sequelize/code/chapter1 [xx:xx:xx]
$ ts-node index.ts
Executing (default): SELECT 1+1 AS result
sequelize &#x5DF2;&#x7ECF;&#x8FDE;&#x63A5;&#x6210;&#x529F;&#x5566;&#xFF01; &gt;_&lt;
</code></pre>
<p>&#x8868;&#x660E;&#x6211;&#x4EEC;&#x6210;&#x529F;&#x8FD0;&#x884C;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="1.写在前面的话.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 写在前面的话">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第一章 搭建环境","level":"1.3","depth":1,"next":{"title":"第二章 模型定义","level":"1.4","depth":1,"ref":"","articles":[{"title":"2.1 实例变量方法","level":"1.4.1","depth":2,"path":"document/3.sequelize 实例变量.md","ref":"./document/3.sequelize 实例变量.md","articles":[]},{"title":"2.2 定义一个模型","level":"1.4.2","depth":2,"path":"document/4.定义一个模型.md","ref":"./document/4.定义一个模型.md","articles":[]},{"title":"2.3 模型字段类型","level":"1.4.3","depth":2,"path":"document/5.模型字段类型.md","ref":"./document/5.模型字段类型.md","articles":[]},{"title":"2.3 导入与配置","level":"1.4.4","depth":2,"path":"document/6.导入与配置.md","ref":"./document/6.导入与配置.md","articles":[]}]},"previous":{"title":"写在前面的话","level":"1.2","depth":1,"path":"document/1.写在前面的话.md","ref":"./document/1.写在前面的话.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"document/2.搭建环境.md","mtime":"2017-06-17T11:37:11.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-06-28T08:29:37.466Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

